<?php

!defined('IN_NOVA') && exit('Access Denied!');

class Link {

	public function start() {
		global $request;

		$action = $request->get( 'p' );

		switch( $action ) {
			case 'addlink':
				$this->add_link();
				break;
			case 'dellink':
				$this->del_link();
				break;
			case 'editlink':
				$this->edit_link();
				break;
			default:
				$this->show( $action );
		}
	}

	private function add_link() {
		global $db,$request,$cache;
		$linkname = $request->get('linkname','P');
		$description = $request->get('linkdescription','P');
		$address = $request->get('linkaddress','P');
		if( $linkname == '' || $description == '' || $address == '' ) {
			echo '<p class="error">错误：您的输入不完整</p>';
			return;
		}
		$db->query( "INSERT INTO `" . DB_PREFIX . "link` (`title`,`description`,`address`) VALUES ('$linkname','$description','$address')" );
		$cache->refresh( 'link', TRUE );
		echo '<p class="warning">添加友情链接成功</p>';
	}

	private function del_link() {
		global $db,$request,$cache;
		$id = $request->id;
		$db->query( "UPDATE `" . DB_PREFIX . "link` SET isdel=1 WHERE lid=$id" );
		$cache->refresh( 'link', TRUE );
		echo '<p class="warning">删除友情链接成功</p>';
	}

	private function edit_link() {
		global $db,$request,$cache;
		$id = $request->get( 'id','P','int' );
		$linkname = $request->get('linkname','P');
		$description = $request->get('linkdescription','P');
		$address = $request->get('linkaddress','P');
		if( $linkname == '' || $description == '' || $address == '' ) {
			echo '<p class="error">错误：您的输入不完整</p>';
			return;
		}
		$db->query( "UPDATE `" . DB_PREFIX . "link` SET title='$linkname',description='$description',address='$address' WHERE lid=$id" );
		$cache->refresh( 'link', TRUE );
		echo '<p class="warning">编辑友情链接成功</p>';
	}

	private function show( $action ) {
		global $cache;
		@header("content-type: text/html; charset=utf-8");
?>
<div class = "admin_panel">
	<?php
	switch( $action ) {
		case 'edit':
			$this->link_edit();
			break;
		default:
			$this->link_list();
	}
	?>
	<script>
		function ajax_get_link( BlogPath, M, P, E, I ){
			$("#admin_loading").css('visibility','visible');
			var URL = BlogPath + 'index.php?m=' + M;
			if( P != null ) {
				URL += '&p=' + P;
			}
			if( E != null ) {
				URL += '&page=' + E;
			}
			if( I != null ) {
				URL += '&id=' + I;
			}
			URL += '&ohash=' + ohash;
			$.get(URL,function(data){
				if(data=='forbidden'){
					document.location.href='./';
					return;
				}
				showMessage(data);
				if( data=='<p class="warning">删除友情链接成功</p>' ){
					setTimeout("ajax_load('<?php echo ADMIN_PATH ?>','link');",200);
				}
				$("#admin_loading").css('visibility','hidden');
			});
		}
		$(document).ready(function(){
			$('#ajax_form').ajaxForm(function(data){
				showMessage(data);
				if( data=='<p class="warning">添加友情链接成功</p>' || data=='<p class="warning">编辑友情链接成功</p>' ){
					setTimeout("ajax_load('<?php echo ADMIN_PATH ?>','link');",200);
				}
				$("#admin_loading").css('visibility','hidden');
			});
		});
	</script>
</div>
<?php
	}

	private function link_edit() {
		global $db,$request;
		$id = $request->id;
		$sql = "SELECT * FROM `" . DB_PREFIX . "link` WHERE lid=$id";
		$value = $db->fetch_one_array( $sql );
?>
<div class = "admin_title">编辑链接</div>
<div class = "admin_content">
	<form action="index.php?m=link&p=editlink" method="post" id="ajax_form">
		<input name="id" type="hidden" value="<?php echo $id ?>"/>
		<table border="0" cellpadding="2" cellspacing="1">
			<tr>
				<td width="180"><div align="right">链接名称&nbsp;&nbsp;</div></td>
				<td align="left"><input name="linkname" type="text" class="main_text" size="30" maxlength="50" value="<?php echo $value['title'] ?>"/></td>
			</tr>
			<tr>
				<td width="180"><div align="right">链接描述&nbsp;&nbsp;</div></td>
				<td align="left"><input name="linkdescription" type="text" class="main_text" size="30" maxlength="100" value="<?php echo $value['description'] ?>"/></td>
			</tr>
			<tr>
				<td width="180"><div align="right">链接地址&nbsp;&nbsp;<div class="main_Tips">必须以"http://"开头&nbsp;&nbsp;</div></div></td>
				<td align="left"><input name="linkaddress" type="text" class="main_text" size="30" maxlength="100"  value="<?php echo $value['address'] ?>"/></td>
			</tr>
			<tr>
				<td width="180"><div align="right"></div></td>
				<td align="left"><input type="submit" class="main_button" value="编辑" /></td>
			</tr>
		</table>
	</form>
</div>
<?php
	}

	private function link_list() {
	global $db;
	$sql = 'SELECT * FROM `' . DB_PREFIX . 'link` WHERE isdel=false ORDER BY lid Asc';
	$link_array = $db->fetch_all( $sql );
?>
<div class = "admin_title"><?php echo L('_LINK_MANAGEMENT_'); ?></div>
<div class = "admin_content">
<div style="padding:5px;margin:2px;">
	<table style="table-layout: fixed;word-wrap: break-word;" width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#bbbbbb">
		<thead>
			<tr align="center" height="22" bgcolor="#ffffff">
				<td width="3%"></td>
				<td width="4%"><?php echo L('_NUM_'); ?></td>
				<td width="14%"><?php echo L('_NAME_'); ?></td>
				<td width="36%"><?php echo L('_DESCRIPTION_'); ?></td>
				<td width="30%"><?php echo L('_ADDRESS_'); ?></td>
				<td width="13%"><?php echo L('_OPERATION_'); ?></td>
			</tr>
		</thead>
		<tbody>
		<?php
			$i = 1;
			foreach( $link_array as $value ) {
		?>
			<tr align="center" height="20" bgcolor="#ffffff">
				<td ></td>
				<td><?php echo $i ?></td>
				<td><?php echo $value['title'] ?></td>
				<td style="text-align:left;padding-left:3px;"><?php echo $value['description'] ?></td>
				<td style="text-align:left;padding-left:3px;"><a href="<?php echo $value['address'] ?>" target="_blank"><?php echo $value['address'] ?></a></td>
				<td>
					<span class="Control">
					<a href="#" onclick="ajax_load('<?php echo ADMIN_PATH ?>','link','edit',null,'<?php echo $value['lid'] ?>');return false;"><?php echo L('_EDIT_'); ?></a> 
					<a href="#" onclick="if(confirm('<?php echo L('_DELETE_CONFIRM_2_'); ?>'))ajax_get_link('<?php echo ADMIN_PATH ?>','link','dellink',null,'<?php echo $value['lid'] ?>');return false;"><?php echo L('_DELETE_'); ?></a>
					</span>
				</td>
			</tr>
		<?php
				$i ++;
			}
		?>
		</tbody>
	</table>
</div>
<fieldset>
	<legend> 添加链接</legend>
	<form action="index.php?m=link&p=addlink" method="post" id="ajax_form">
		<table border="0" cellpadding="2" cellspacing="1">
			<tr>
				<td width="180"><div align="right">链接名称&nbsp;&nbsp;</div></td>
				<td align="left"><input name="linkname" type="text" class="main_text" size="30" maxlength="50"/></td>
			</tr>
			<tr>
				<td width="180"><div align="right">链接描述&nbsp;&nbsp;</div></td>
				<td align="left"><input name="linkdescription" type="text" class="main_text" size="30" maxlength="100"/></td>
			</tr>
			<tr>
				<td width="180"><div align="right">链接地址&nbsp;&nbsp;<div class="main_Tips">必须以"http://"开头&nbsp;&nbsp;</div></div></td>
				<td align="left"><input name="linkaddress" type="text" class="main_text" size="30" maxlength="100" /></td>
			</tr>
			<tr>
				<td width="180"><div align="right"></div></td>
				<td align="left"><input type="submit" class="main_button" value="添加" /></td>
			</tr>
		</table>
	</form>
</fieldset>
</div>
<?php
	}
}
?>
